package com.summer.ChengXuYuanMianShiJingDian;

import com.summer.ListNode;

import java.util.List;

public class Q2_5 {
    public static void main(String[] args) {
        //243 564
        ListNode head1 = new ListNode(2);
        ListNode head2 = new ListNode(5);
        ListNode tail1 =head1;
        ListNode tail2 =head2;
        ListNode listNode1 = new ListNode(4);
        ListNode listNode2 = new ListNode(3);

        ListNode listNode3 = new ListNode(6);
        ListNode listNode4 = new ListNode(4);
      //  ListNode listNode5 = new ListNode(2);
        tail1.next=listNode1;
        listNode1.next=listNode2;
        tail2.next = listNode3;
        listNode3.next=listNode4;
        ListNode head3 = new ListNode(1);
        ListNode head4 = new ListNode(9);
        ListNode head5 = new ListNode(9);
        head4.next=head5;
        ListNode addTwoNumbers = addTwoNumbers(head3, head4);
        while(addTwoNumbers!=null){
            if (addTwoNumbers.next==null) System.out.print(addTwoNumbers.val);
            else System.out.print(addTwoNumbers.val+"---->");
            addTwoNumbers = addTwoNumbers.next;
        }
    }
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head  = new ListNode(0); //[2,4,3][5,6,4]
        // 0707
        ListNode tail = head;
        boolean flag  = false;
        while(l1!=null&&l2!=null){
            int val = 0;
            if (flag){
                val++;
                flag = false;
            }
            if((l1.val+l2.val+val)>=10){
                val =(l1.val+l2.val+val)%10;
                flag = true;
            }else {
                val = l1.val+l2.val+val;
            }
            ListNode node = new ListNode(val);
            tail.next = node;
            tail = tail.next;
            l1 = l1.next;
            l2 = l2.next;
        }

        while (l1==null&&l2!=null){
            int val = 0;
            if (flag){
                val++;
                flag = false;
            }
            if((val+l2.val)>=10){
                val = (val+l2.val)%10;
                flag = true;
            }else {
                val = val+l2.val;
            }
            ListNode node = new ListNode(val);
            tail.next = node;
            tail = tail.next;
            l2 = l2.next;
        }
        while (l1!=null&&l2==null){
            int val = 0;
            if (flag){
                val++;
                flag = false;
            }
            if((val+l1.val)>=10){
                val = (val+l1.val)%10;
                flag = true;
            }else {
                val = val+l1.val;
            }
            ListNode node = new ListNode(val);
            tail.next = node;
            tail = tail.next;
            l1 = l1.next;
        }
        if (l1==null&&l2==null){
            int val = 0;
            if (flag){
                val++;
                ListNode node = new ListNode(val);
                tail.next = node;
            }

        }

        return head.next;
    }
}
